home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / ViewConnect.cpp < prev    next >
C/C++ Source or Header  |  2000-07-15  |  27KB  |  844 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. // ViewConnect.cpp : implementation of the CViewConnect class
  30. //
  31.  
  32. #include "stdafx.h"
  33. #include "Gnucleus.h"
  34.  
  35. #include "GnucleusDoc.h"
  36. #include "ViewConnect.h"
  37. #include "ViewSearch.h"
  38. #include "MainFrm.h"
  39.  
  40. #include "GnuSock.h"
  41. #include "GnuHash.h"
  42. #include "GnuControl.h"
  43.  
  44. #include "IPFilter.h"
  45.  
  46. #ifdef _DEBUG
  47. #define new DEBUG_NEW
  48. #undef THIS_FILE
  49. static char THIS_FILE[] = __FILE__;
  50. #endif
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CViewConnect
  54.  
  55. IMPLEMENT_DYNCREATE(CViewConnect, CFormView)
  56.  
  57. BEGIN_MESSAGE_MAP(CViewConnect, CFormView)
  58.     //{{AFX_MSG_MAP(CViewConnect)
  59.     ON_WM_SIZE()
  60.     ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
  61.     ON_WM_TIMER()
  62.     ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
  63.     ON_NOTIFY(NM_DBLCLK, IDC_LST_CONNECTED, OnDblclkLstConnected)
  64.     ON_NOTIFY(NM_DBLCLK, IDC_LST_CACHE, OnDblclkLstCache)
  65.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  66.     ON_EN_CHANGE(IDC_EDIT_PORT, OnChangeEditPort)
  67.     //}}AFX_MSG_MAP
  68. END_MESSAGE_MAP()
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CViewConnect construction/destruction
  72.  
  73. CViewConnect::CViewConnect()
  74.     : CFormView(CViewConnect::IDD)
  75. {
  76.     //{{AFX_DATA_INIT(CViewConnect)
  77.     //}}AFX_DATA_INIT
  78.  
  79.     btnSearch = NULL;
  80.     cbKeyword = NULL;
  81.     cbSpeed   = NULL;
  82. }
  83.  
  84. CViewConnect::~CViewConnect()
  85. {
  86.     //KillTimer(UpdateTimer);
  87. }
  88.  
  89. void CViewConnect::DoDataExchange(CDataExchange* pDX)
  90. {
  91.     CFormView::DoDataExchange(pDX);
  92.     //{{AFX_DATA_MAP(CViewConnect)
  93.     DDX_Control(pDX, IDC_COMBO_HOST, m_cbxHost);
  94.     DDX_Control(pDX, IDC_BUTTON_ADD, m_btnAdd);
  95.     DDX_Control(pDX, IDC_BUTTON_REMOVE, m_btnRemove);
  96.     DDX_Control(pDX, IDC_EDIT_PORT, m_ebPort);
  97.     DDX_Control(pDX, IDC_STATIC_CONNECT, m_stcConnect);
  98.     DDX_Control(pDX, IDC_LST_CACHE, m_lstCache);
  99.     DDX_Control(pDX, IDC_LST_CONNECTED, m_lstConnected);
  100.     DDX_Control(pDX, IDC_STATIC_ADD, m_stcAdd);
  101.     DDX_Control(pDX, IDC_STATIC_EXIST, m_stcExist);
  102.     DDX_Control(pDX, IDC_STATIC_HOST, m_stcHost);
  103.     DDX_Control(pDX, IDC_STATIC_PORT, m_stcPort);
  104.     //}}AFX_DATA_MAP
  105. }
  106.  
  107. BOOL CViewConnect::PreCreateWindow(CREATESTRUCT& cs)
  108. {
  109.     // TODO: Modify the Window class or styles here by modifying
  110.     //  the CREATESTRUCT cs
  111.  
  112.     return CFormView::PreCreateWindow(cs);
  113. }
  114.  
  115. void CViewConnect::OnInitialUpdate()
  116. {
  117.     CFormView::OnInitialUpdate();
  118.     ResizeParentToFit();
  119.  
  120.     // Initialize the list control
  121.     int offSet;
  122.  
  123.     if(m_lstConnected.GetScrollLimit(SB_VERT))
  124.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  125.     else
  126.         offSet = 4;
  127.  
  128.     CRect rect;
  129.     m_lstConnected.GetWindowRect(&rect);
  130.  
  131.     m_lstConnected.InsertColumn(0, "Node", LVCFMT_LEFT,
  132.         (rect.Width() - offSet) * 2/5, 0);
  133.     m_lstConnected.InsertColumn(1, "Port", LVCFMT_LEFT,
  134.         (rect.Width() - offSet) * 1/5, 1);
  135.     m_lstConnected.InsertColumn( 2, "Status", LVCFMT_LEFT,
  136.         (rect.Width() - offSet) * 2/5, 2);
  137.  
  138.     m_lstConnected.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  139.  
  140.     
  141.     if(m_lstCache.GetScrollLimit(SB_VERT))
  142.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  143.     else
  144.         offSet = 4;
  145.  
  146.     m_lstCache.GetWindowRect(&rect);
  147.     
  148.     m_lstCache.InsertColumn(0, "Node", LVCFMT_LEFT,
  149.         (rect.Width() - offSet) * 3/5, 0);
  150.     m_lstCache.InsertColumn(1, "Port", LVCFMT_RIGHT,
  151.         (rect.Width() - offSet) * 1/5, 1);
  152.     m_lstCache.InsertColumn(2, "Ping", LVCFMT_RIGHT,
  153.         (rect.Width() - offSet) * 1/5, 2);
  154.  
  155.     m_lstCache.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  156.  
  157.  
  158.     // Fill cache with saved hosts
  159.     int index, posLst;
  160.     CString HostPort, Host, Port;
  161.     POSITION pos = GetDocument()->SavedHosts.GetHeadPosition();
  162.  
  163.     for (int i = 0; i < GetDocument()->SavedHosts.GetCount();i++)
  164.     {
  165.         HostPort = GetDocument()->SavedHosts.GetAt(pos);
  166.  
  167.         index = HostPort.Find(":", 0);
  168.         Host = HostPort.Mid(0, index);
  169.         Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
  170.  
  171.         posLst = m_lstCache.GetItemCount();
  172.         m_lstCache.InsertItem(posLst, "");
  173.         m_lstCache.SetItemText(posLst, m_lstCache.GetColumnNumber( "Node" ), Host);
  174.         m_lstCache.SetItemText(posLst, m_lstCache.GetColumnNumber( "Port" ), Port);
  175.  
  176.         GetDocument()->SavedHosts.GetNext(pos);
  177.     }
  178.  
  179.    m_cbxHost.AddString(_T("gnet.ath.cx"));
  180.    m_cbxHost.AddString(_T("gnet2.ath.cx"));
  181.    m_cbxHost.AddString(_T("gnet3.ath.cx"));
  182.    m_cbxHost.AddString(_T("gnet4.ath.cx"));
  183.    m_cbxHost.AddString(_T("gnet5.ath.cx"));
  184.     // Set Buttons and edit boxes
  185.     //btnSearch = (CButton *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_BUTTON_SEARCH);
  186.     //cbKeyword = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
  187.     //cbSpeed = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SPEED);
  188.  
  189.     //btnSearch->EnableWindow(FALSE);
  190.     //cbKeyword->EnableWindow(FALSE);
  191.     //cbSpeed->EnableWindow(FALSE);
  192.  
  193.     m_btnRemove.EnableWindow(FALSE);
  194.     m_ebPort.SetWindowText("6346");
  195.  
  196.     // Start the timer
  197.     UpdateTimer = SetTimer(1, 500, 0);
  198.     oneSec = 1;
  199.  
  200.     GetParentFrame()->GetClientRect(&rect);
  201.     OnSize(SIZE_RESTORED, rect.right - 4, rect.bottom - 4);
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // CViewConnect diagnostics
  206.  
  207. #ifdef _DEBUG
  208. void CViewConnect::AssertValid() const
  209. {
  210.     CFormView::AssertValid();
  211. }
  212.  
  213. void CViewConnect::Dump(CDumpContext& dc) const
  214. {
  215.     CFormView::Dump(dc);
  216. }
  217.  
  218. CGnucleusDoc* CViewConnect::GetDocument() // non-debug version is inline
  219. {
  220.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGnucleusDoc)));
  221.     return (CGnucleusDoc*)m_pDocument;
  222. }
  223. #endif //_DEBUG
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CViewConnect message handlers
  227.  
  228. // Memory leaks when shutting down soon after starting.
  229. // CViewConnect::OnSize and CViewSearch::OnSize and CViewTransfer::OnSize should have
  230. // the LPRECT vars changes to RECT var to avoid allocation with "new". This keeps
  231. // memory leaks from occuring in this code. Here is the code to save you some time.
  232.  
  233. void CViewConnect::OnSize(UINT nType, int cx, int cy) 
  234. // leave all hope behind those who enter here
  235. {
  236.     if( cx < 580 || cy < 266)        // too small and formatting goes to hell.
  237.         ;                            // so just ignore the size change.
  238.  
  239.     else if(m_lstCache.m_hWnd != NULL)
  240.     {
  241.         int top_LstCache,      left_LstCache,
  242.             top_LstConnected, left_LstConnected,
  243.             top_StcConnect,   left_StcConnect,   right_StcConnect, bottom_StcConnect,
  244.             top_BtnRemove,    left_BtnRemove,    right_BtnRemove,  bottom_BtnRemove,
  245.             top_BtnAdd,       left_BtnAdd,       right_BtnAdd,     bottom_BtnAdd, 
  246.             top_StcAdd,       left_StcAdd,       right_StcAdd,     bottom_StcAdd,
  247.             top_StcHost,      left_StcHost,      right_StcHost,    bottom_StcHost,
  248.             top_StcPort,      left_StcPort,      right_StcPort,    bottom_StcPort,
  249.             top_cbxHost,      left_cbxHost,      right_cbxHost,    bottom_cbxHost,
  250.             top_EbPort,       left_EbPort,       right_EbPort,     bottom_EbPort;
  251.  
  252.         // I'm scared
  253.     // DW - new was being used - caused memory leaks 
  254.         RECT rect_Wnd,         
  255.                rect_LstCache,    
  256.                rect_LstConnected,
  257.                rect_StcConnect,  
  258.                rect_BtnRemove,   
  259.                rect_BtnAdd,      
  260.                rect_StcAdd,      
  261.                rect_StcHost,     
  262.                rect_StcPort,     
  263.                rect_cbxHost,      
  264.                rect_EbPort;      
  265.  
  266.         GetWindowRect(&rect_Wnd);
  267.         m_lstCache.GetWindowRect(&rect_LstCache);
  268.         m_lstConnected.GetWindowRect(&rect_LstConnected);
  269.         m_stcConnect.GetWindowRect(&rect_StcConnect);
  270.         m_btnRemove.GetWindowRect(&rect_BtnRemove);
  271.         m_btnAdd.GetWindowRect(&rect_BtnAdd);
  272.         m_stcAdd.GetWindowRect(&rect_StcAdd);
  273.         m_stcHost.GetWindowRect(&rect_StcHost);
  274.         m_stcPort.GetWindowRect(&rect_StcPort);
  275.         m_cbxHost.GetWindowRect(&rect_cbxHost);
  276.         m_ebPort.GetWindowRect(&rect_EbPort);
  277.  
  278.         // oh god, please no
  279.         top_LstCache  = rect_LstCache.top  - rect_Wnd.top  - 2;
  280.         left_LstCache = rect_LstCache.left - rect_Wnd.left - 2;
  281.  
  282.         top_LstConnected  = rect_LstConnected.top  - rect_Wnd.top  - 2;
  283.         left_LstConnected = rect_LstConnected.left - rect_Wnd.left - 2;
  284.  
  285.         top_StcConnect    = rect_StcConnect.top    - rect_Wnd.top  - 2;
  286.         left_StcConnect   = rect_StcConnect.left   - rect_Wnd.left - 2;
  287.         right_StcConnect  = rect_StcConnect.right  - rect_StcConnect.left;
  288.         bottom_StcConnect = rect_StcConnect.bottom - rect_StcConnect.top;
  289.  
  290.         top_BtnRemove    = rect_BtnRemove.top    - rect_Wnd.top  - 2;
  291.         left_BtnRemove   = rect_BtnRemove.left   - rect_Wnd.left - 2;
  292.         right_BtnRemove  = rect_BtnRemove.right  - rect_BtnRemove.left;
  293.         bottom_BtnRemove = rect_BtnRemove.bottom - rect_BtnRemove.top;
  294.  
  295.         top_BtnAdd    = rect_BtnAdd.top    - rect_Wnd.top  - 2;
  296.         left_BtnAdd   = rect_BtnAdd.left   - rect_Wnd.left - 2;
  297.         right_BtnAdd  = rect_BtnAdd.right  - rect_BtnAdd.left;
  298.         bottom_BtnAdd = rect_BtnAdd.bottom - rect_BtnAdd.top;
  299.  
  300.         // it's coming back...
  301.         top_StcAdd    = rect_StcAdd.top    - rect_Wnd.top  - 2;
  302.         left_StcAdd   = rect_StcAdd.left   - rect_Wnd.left - 2;
  303.         right_StcAdd  = rect_StcAdd.right  - rect_StcAdd.left;
  304.         bottom_StcAdd = rect_StcAdd.bottom - rect_StcAdd.top;
  305.  
  306.         top_StcHost    = rect_StcHost.top    - rect_Wnd.top  - 2;
  307.         left_StcHost   = rect_StcHost.left   - rect_Wnd.left - 2;
  308.         right_StcHost  = rect_StcHost.right  - rect_StcHost.left;
  309.         bottom_StcHost = rect_StcHost.bottom - rect_StcHost.top;
  310.  
  311.         top_StcPort    = rect_StcPort.top    - rect_Wnd.top  - 2;
  312.         left_StcPort   = rect_StcPort.left   - rect_Wnd.left - 2;
  313.         right_StcPort  = rect_StcPort.right  - rect_StcPort.left;
  314.         bottom_StcPort = rect_StcPort.bottom - rect_StcPort.top;
  315.  
  316.         top_cbxHost    = rect_cbxHost.top    - rect_Wnd.top  - 2;
  317.         left_cbxHost   = rect_cbxHost.left   - rect_Wnd.left - 2;
  318.         right_cbxHost  = rect_cbxHost.right  - rect_cbxHost.left;
  319.         bottom_cbxHost = rect_cbxHost.bottom - rect_cbxHost.top;
  320.  
  321.         top_EbPort    = rect_EbPort.top    - rect_Wnd.top  - 2;
  322.         left_EbPort   = rect_EbPort.left   - rect_Wnd.left - 2;
  323.         right_EbPort  = rect_EbPort.right  - rect_EbPort.left;
  324.         bottom_EbPort = rect_EbPort.bottom - rect_EbPort.top;
  325.  
  326.         // the horrors you've witnessed, the horrors you will now
  327.         m_lstCache.MoveWindow(left_LstCache, top_LstCache, cx / 2 - 10, cy - top_LstCache - bottom_BtnAdd - bottom_StcAdd - 30);
  328.         m_lstConnected.MoveWindow(cx / 2 + 10, top_LstCache, cx / 2 - 17, cy - top_LstConnected - bottom_BtnRemove - 21);
  329.         m_stcConnect.MoveWindow(cx / 2 + 10, top_StcConnect, right_StcConnect, bottom_StcConnect);
  330.         m_btnRemove.MoveWindow(cx / 4 * 3 - (right_BtnRemove / 2), cy - bottom_BtnRemove - 7, right_BtnRemove, bottom_BtnRemove);
  331.         m_btnAdd.MoveWindow(cx / 4 - (right_BtnAdd / 2), cy - bottom_BtnAdd - 7, right_BtnAdd, bottom_BtnAdd);
  332.         m_stcAdd.MoveWindow(left_StcAdd, cy - bottom_BtnAdd - bottom_StcAdd - 22, cx / 2 - 10, bottom_StcAdd);
  333.         m_stcHost.MoveWindow(left_StcHost, cy - bottom_BtnAdd - bottom_StcAdd - 13 + bottom_StcAdd / 5, right_StcHost, bottom_StcHost);
  334.         m_stcPort.MoveWindow(left_StcPort, cy - bottom_BtnAdd - bottom_StcAdd - 18 + 3 * bottom_StcAdd / 5, right_StcPort, bottom_StcPort);
  335.         m_cbxHost.MoveWindow(left_cbxHost, cy - bottom_BtnAdd - bottom_StcAdd - 13 + bottom_StcAdd / 5, (cx / 2 - 10) / 2, bottom_cbxHost);
  336.         m_ebPort.MoveWindow(left_EbPort, cy - bottom_BtnAdd - bottom_StcAdd - 18 + 3 * bottom_StcAdd / 5, right_EbPort, bottom_EbPort);
  337.  
  338.         // the darkness has put out the light
  339.         
  340.         int offSet;
  341.  
  342.         if(m_lstConnected.GetScrollLimit(SB_VERT))
  343.             offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  344.         else
  345.             offSet = 4;
  346.  
  347.         CRect rect;
  348.         m_lstConnected.GetWindowRect(&rect);
  349.  
  350.         m_lstConnected.SetColumnWidth(0, (rect.Width() - offSet) * 2/5);
  351.         m_lstConnected.SetColumnWidth(1, (rect.Width() - offSet) * 1/5);
  352.         m_lstConnected.SetColumnWidth(2, (rect.Width() - offSet) * 2/5);
  353.  
  354.         if(m_lstCache.GetScrollLimit(SB_VERT))
  355.             offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  356.         else
  357.             offSet = 4;
  358.  
  359.         m_lstCache.GetWindowRect(&rect);
  360.  
  361.         m_lstCache.SetColumnWidth(0, (rect.Width() - offSet) * 3/5);
  362.         m_lstCache.SetColumnWidth(1, (rect.Width() - offSet) * 1/5);
  363.         m_lstCache.SetColumnWidth(2, (rect.Width() - offSet) * 1/5);
  364.     }
  365.  
  366.     CFormView::OnSize(nType, cx, cy);
  367. }
  368.  
  369.  
  370. void CViewConnect::OnButtonAdd() 
  371. {
  372.     CString Host, Port;
  373.     int index = -1;
  374.  
  375.    m_cbxHost.GetWindowText(Host);
  376.     //m_ebHost.GetWindowText(Host);
  377.     m_ebPort.GetWindowText(Port);
  378.  
  379.     // accept a host:port format.
  380.     if( (index = Host.Find(_T(":"), 0)) != -1)
  381.     {
  382.         Port = Host.Mid(index + 1, Host.GetLength() - index);
  383.         Host = Host.Mid(0, index);
  384.  
  385.         //m_ebHost.SetWindowText(Host);
  386.       m_cbxHost.SetWindowText(Host);
  387.         m_ebPort.SetWindowText(Port);
  388.     }
  389.     if(Host == "" || Port == "")
  390.         return;
  391.  
  392.     AddConnect(Host, Port);    
  393. }
  394.  
  395. BOOL CViewConnect::AddConnect(CString Host, CString Port)
  396. {
  397.     for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
  398.     {
  399.         if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber("Node" ) ) == Host &&
  400.            m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber("Port" ) ) == Port)
  401.         {
  402.             //AfxMessageBox("Host already connected.");
  403.             return FALSE;  // DW return value
  404.         }
  405.     }
  406.  
  407.     int pos = m_lstConnected.GetItemCount();
  408.     m_lstConnected.InsertItem(pos, "");
  409.     m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Node" ), Host);
  410.     m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Port" ), Port);
  411.     m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Status" ), "Connecting");
  412.  
  413.   // DW return new BOOL result
  414.     return GetDocument()->GnuComm->AddNode(Host, Port);
  415. }
  416.  
  417.  
  418. void CViewConnect::OnButtonRemove() 
  419. {
  420.     int nItem;
  421.     CString Host, Port;
  422.     POSITION pos = m_lstConnected.GetFirstSelectedItemPosition();
  423.     
  424.     CString Status = "Disconnecting";
  425.  
  426.     while(pos != NULL)
  427.     {
  428.         nItem = m_lstConnected.GetNextSelectedItem(pos);
  429.  
  430.         Host = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber("Node" ) ); 
  431.         Port = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber("Port" ) ); 
  432.  
  433.         GetDocument()->QueueDisconnect.AddTail(Host + ":" + Port);
  434.  
  435.         m_lstConnected.SetItemText(nItem, m_lstConnected.GetColumnNumber( "Status"), Status);
  436.     }
  437.  
  438.     if(m_lstConnected.GetItemCount() == 0)
  439.     {
  440.         //btnSearch->EnableWindow(FALSE);
  441.         //cbKeyword->EnableWindow(FALSE);
  442.         //cbSpeed->EnableWindow(FALSE);
  443.  
  444.         m_btnRemove.EnableWindow(FALSE);
  445.     }
  446. }
  447.  
  448. // ViewConnect.cpp - OnTimer call should not be re-entrant
  449. void CViewConnect::OnTimer(UINT nIDEvent) 
  450. {
  451.     int index;
  452.     CString Host, Port, HostPort, Status;
  453.     // DW - Not re-entrant
  454.     static bool bInFunc = FALSE;
  455.     
  456.     CFormView::OnTimer(nIDEvent);
  457.     
  458.     if (bInFunc)
  459.         return;
  460.  
  461.     bInFunc = TRUE;
  462.  
  463.  
  464.     oneSec = oneSec % 1 ? 1 : 2;
  465.  
  466.     //btnSearch = (CButton *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_BUTTON_SEARCH);
  467.     //cbKeyword = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
  468.     //cbSpeed = (CComboBox *) ( (CMainFrame *) AfxGetApp()->m_pMainWnd )->GetDialogBar()->GetDlgItem(IDC_COMBO_SPEED);
  469.  
  470.  
  471.     if(nIDEvent == UpdateTimer)
  472.     {
  473.         
  474.         // Update Connects
  475.         while(!GetDocument()->QueueConnect.IsEmpty())
  476.         {
  477.             // Break the string into two parts
  478.             HostPort = GetDocument()->QueueConnect.RemoveHead();
  479.             
  480.             index = HostPort.Find(":", 0);
  481.             Host = HostPort.Mid(0, index);
  482.             Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
  483.  
  484.             for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
  485.             {
  486.                 // Connecting and outgoing node
  487.                 if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) ) == Host &&
  488.                    m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) ) == Port)
  489.                 {
  490.                    m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber("Status" ), "Connected");
  491.                 }
  492.             }
  493.             // Connecting an incomming node
  494.             if(Port == "Inbound")
  495.             {
  496.                 int pos = m_lstConnected.GetItemCount();
  497.                 m_lstConnected.InsertItem(pos, "" );
  498.                 m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Node" ) , Host);
  499.                 m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Port" ) , Port);
  500.                 m_lstConnected.SetItemText(pos, m_lstConnected.GetColumnNumber( "Status" ) , "Connected");
  501.             }
  502.  
  503.             // Send existing search queries out to the newly connected node    
  504.             // FIXME, it doesnt work =)
  505.             CString Title;
  506.             POSITION pos = GetDocument()->GetFirstViewPosition();
  507.  
  508.             while (pos != NULL)
  509.             {
  510.                 CView* pView = GetDocument()->GetNextView(pos);
  511.                 pView->GetParentFrame()->GetWindowText(Title);
  512.  
  513.                 if(Title.Mid(0, 9) == "Searching")
  514.                 {
  515.                     CGnuSock *node = GetDocument()->GnuComm->GetNode(HostPort);
  516.                     node->Send_Query( (CViewSearch *) pView); 
  517.                 }
  518.             }
  519.  
  520.             // Update the host count
  521.             GetDocument()->TotalHosts = GetDocument()->Connections.GetCount();
  522.             
  523.             CGnuSock *newNode = GetDocument()->GnuComm->GetNode(HostPort);
  524.  
  525.             if(newNode != NULL)
  526.                 newNode->RefreshHostCount();
  527.         }
  528.  
  529.         // Update nodes Connecting
  530.         CString MaxDots;
  531.         for(int i = 0; i < GetDocument()->m_TimeoutConnect; i++)
  532.             MaxDots += '.';
  533.         
  534.         for(i = 0; i < m_lstConnected.GetItemCount(); i++)
  535.         {
  536.             Status = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) );
  537.  
  538.             Host = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) ); 
  539.             Port = m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) ); 
  540.             
  541.             if(Status.Find("Connecting") != -1)
  542.             {
  543.                 if(Status.Find(MaxDots) != -1)
  544.                 {
  545.                     CGnuSock* sock = GetDocument()->GnuComm->GetNode (CString(Host + ":" + Port));
  546.  
  547.                     if (sock)
  548.                     {
  549.                         Status = "Disconnecting";
  550.                         m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
  551.                         sock->WantToDisconnect();
  552.                         
  553.                         
  554.                         if (sock->m_CanRelease)
  555.                         {
  556.                             GetDocument()->GnuComm->RemoveNode(Host, Port);
  557.  
  558.                             sock = NULL;
  559.                         }
  560.                     }
  561.  
  562.                     if (!sock)
  563.                     {
  564.                         m_lstConnected.DeleteItem(i);
  565.                     }
  566.                 }
  567.                 else
  568.                 {
  569.                     if(oneSec == 2)
  570.                         Status += ".";
  571.                     m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);    
  572.                 }
  573.             }
  574.             // well it's not connect, so it must be:
  575.             else if(Status.Find("Disconnecting") != -1)
  576.             {
  577.                 CGnuSock* sock = GetDocument()->GnuComm->GetNode (Host + ":" + Port);
  578.  
  579.                 if(sock)
  580.                 {
  581.                     sock->WantToDisconnect();
  582.  
  583.                     // If it's been this long and there has been no OnClose, then kill it anyways.
  584.                     // or if it has been "released" as in shutdown now!
  585.                     if (sock->m_CanRelease || Status.Find(".") != -1)
  586.                     {
  587.                         GetDocument()->GnuComm->RemoveNode(Host, Port);
  588.  
  589.                         sock = NULL;
  590.                     }
  591.                 }
  592.                 // if the socket was invalid to beginwith, or has been killed, then remove from list.
  593.                 if (!sock)    // not else, because sock can change
  594.                 {
  595.                     m_lstConnected.DeleteItem(i);
  596.                     continue;
  597.                 }
  598.  
  599.                 // Update the exclamation marks.
  600.                 if(oneSec == 2)
  601.                 {
  602.                     Status += ".";
  603.                     m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);    
  604.                 }
  605.             }
  606.             
  607.         }
  608.  
  609.         // Update Disconnects.  This queue gets added to by CleanUp
  610.         while(!GetDocument()->QueueDisconnect.IsEmpty())
  611.         {
  612.             // Break the string into two parts
  613.             HostPort = GetDocument()->QueueDisconnect.RemoveHead();
  614.             
  615.             index = HostPort.Find(":", 0);
  616.             Host = HostPort.Mid(0, index);
  617.             Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
  618.  
  619.             CGnuSock* node = GetDocument()->GnuComm->GetNode (HostPort);
  620.  
  621.             for(int i = 0; i < m_lstConnected.GetItemCount(); i++)
  622.             {
  623.                 if(m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Node" ) ) == Host &&
  624.                    m_lstConnected.GetItemText(i, m_lstConnected.GetColumnNumber( "Port" ) ) == Port)
  625.                 {
  626.                     if(node && !node->m_CanRelease )    // change text, and let loop above get it.
  627.                     {
  628.                         CString Status = "Disconnecting";
  629.                         m_lstConnected.SetItemText(i, m_lstConnected.GetColumnNumber( "Status" ) , Status);
  630.                     }
  631.                     else    // invalid, so delete it
  632.                     {
  633.                         GetDocument()->GnuComm->RemoveNode (Host, Port);
  634.                         m_lstConnected.DeleteItem(i);
  635.                     }
  636.                     break;
  637.                 }
  638.             }
  639.  
  640.             // Update the host count            
  641.             GetDocument()->TotalHosts = GetDocument()->Connections.GetCount();
  642.  
  643.         }
  644.  
  645.         GetDocument()->RemoveEarliestHostPort(&HostPort);
  646.         if (!HostPort.IsEmpty())
  647.         {
  648.         // Update the Host cache
  649. //        if(!GetDocument()->QueueCache.IsEmpty())
  650. //        {
  651. //            HostPort = GetDocument()->QueueCache.RemoveTail();
  652.             
  653.             index = HostPort.Find(":", 0);
  654.             Host = HostPort.Mid(0, index);
  655.             Port = HostPort.Mid(index + 1, HostPort.GetLength() - index);
  656.  
  657.             // See if its a duplicate
  658.             LVFINDINFO *findInfo = new LVFINDINFO;
  659.  
  660.             findInfo->flags = LVFI_STRING;
  661.             findInfo->psz = Host;
  662.  
  663.             if(m_lstCache.FindItem(findInfo) == -1 && 
  664.                 CIPFilter::AllowIP( StrtoIP(Host) ) &&
  665.                 CIPFilter::IsPrivateIP(StrtoIP(Host), StrtoIP(Host)) == 0 )
  666.             {
  667.                 // Insert the item
  668.                 m_lstCache.InsertItem(0, "");
  669.                 m_lstCache.SetItemText(0, m_lstCache.GetColumnNumber( "Node" ) , Host);
  670.                 m_lstCache.SetItemText(0, m_lstCache.GetColumnNumber( "Port" ) , Port);
  671.  
  672.                 if(m_lstCache.GetItemCount() > 500)
  673.                     m_lstCache.DeleteItem(500);
  674.             }
  675.  
  676.             delete findInfo;
  677.         }
  678.  
  679.         while(m_lstCache.GetItemCount() > 500)
  680.             m_lstCache.DeleteItem(500);
  681.  
  682.         // Do some 'intelligent' node management
  683.         if(GetDocument()->m_MonitorType != 0)
  684.         {
  685.             int connects = GetDocument()->Connections.GetCount(), 
  686.                 optimal  = GetDocument()->m_ConnectNum;
  687.  
  688.             if(GetDocument()->m_MonitorType == 1)
  689.                 if(connects >= optimal)
  690.                     RemoveFirstNode();
  691.             if(GetDocument()->m_MonitorType == 2)
  692.             {
  693.                 if(connects > optimal)
  694.                     RemoveFirstNode();
  695.                 if(connects < optimal)
  696.                     if (!ConnectRandomNode()) // DW - No need to do rest if no connect
  697.                     {
  698.                         bInFunc = FALSE;
  699.                         return;
  700.                     }
  701.             }
  702.  
  703.             if(GetDocument()->m_MonitorType == 3)
  704.                 if(connects <= optimal)
  705.                     if (!ConnectRandomNode()) // DW - No need to do rest if no connect
  706.                     {
  707.                         bInFunc = FALSE;
  708.                         return;
  709.                     }
  710.         }
  711.  
  712.         CString Title = "Connected to ";
  713.                 Title += CommaIze(DWrdtoStr( GetDocument()->GnuComm->GetHostCount()) );
  714.                 Title += " Nodes";
  715.  
  716.         GetParentFrame()->SetWindowText(Title);
  717.  
  718.         if(m_lstConnected.GetItemCount())
  719.         {
  720.             //btnSearch->EnableWindow();
  721.             //cbKeyword->EnableWindow();
  722.             //cbSpeed->EnableWindow();
  723.  
  724.             m_btnRemove.EnableWindow();
  725.         }
  726.         else
  727.         {
  728.             //btnSearch->EnableWindow(FALSE);
  729.             //cbKeyword->EnableWindow(FALSE);
  730.             //cbSpeed->EnableWindow(FALSE);
  731.  
  732.             m_btnRemove.EnableWindow(FALSE);
  733.         }
  734.     }
  735.  
  736.     bInFunc = FALSE;
  737. }
  738.  
  739. void CViewConnect::OnDblclkLstConnected(NMHDR* pNMHDR, LRESULT* pResult) 
  740. {
  741.     LPNMITEMACTIVATE Control = (LPNMITEMACTIVATE) pNMHDR;
  742.  
  743.     int nItem = m_lstConnected.HitTest(Control->ptAction);
  744.     CString Host, Port, HostPort, ViewHostPort, Title = "Node ";
  745.  
  746.     if(nItem != -1)
  747.     {
  748.         Host = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber( "Node" ) );
  749.         Port = m_lstConnected.GetItemText(nItem, m_lstConnected.GetColumnNumber( "Port" ) );
  750.         HostPort = Host;
  751.         HostPort += ":";
  752.         HostPort += Port;
  753.         Title += HostPort;
  754.  
  755.         POSITION pos = GetDocument()->GetFirstViewPosition();
  756.  
  757.         // Make sure window does not already exist
  758.         while (pos != NULL)
  759.         {
  760.             CView* pView = GetDocument()->GetNextView(pos);
  761.             
  762.             pView->GetParentFrame()->GetWindowText(ViewHostPort);
  763.  
  764.             if(ViewHostPort == Title)
  765.             {
  766.                 pView->GetParentFrame()->BringWindowToTop();
  767.             
  768.                 return;
  769.             }
  770.         }
  771.  
  772.         CFrameWnd* pNewFrame = ((CGnucleusApp *) AfxGetApp())->m_pNodeViewTemplate->CreateNewFrame(((CGnucleusApp *) AfxGetApp())->MasterDoc, NULL);
  773.  
  774.         pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
  775.         pNewFrame->SetWindowText(Title);
  776.  
  777.         ((CGnucleusApp *) AfxGetApp())->m_pNodeViewTemplate->InitialUpdateFrame(pNewFrame, ((CGnucleusApp *) AfxGetApp())->MasterDoc);
  778.     }
  779.  
  780.     *pResult = 0;
  781. }
  782.  
  783. void CViewConnect::OnDblclkLstCache(NMHDR* pNMHDR, LRESULT* pResult) 
  784. {
  785.     LPNMITEMACTIVATE Control = (LPNMITEMACTIVATE) pNMHDR;
  786.  
  787.     int nItem = m_lstCache.HitTest(Control->ptAction);
  788.     CString Host, Port;
  789.  
  790.     if(nItem != -1)
  791.     {
  792.         Host = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Node" ) );
  793.         Port = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Port" ) );
  794.  
  795.         AddConnect(Host, Port);
  796.     }
  797.     
  798.     *pResult = 0;
  799. }
  800.  
  801. BOOL CViewConnect::ConnectRandomNode()
  802. {
  803.     if(m_lstCache.GetItemCount())
  804.     {
  805.         int nItem = rand() % m_lstCache.GetItemCount();
  806.  
  807.         CString Host = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Node" ) );
  808.         CString Port = m_lstCache.GetItemText(nItem, m_lstCache.GetColumnNumber( "Port" ) );
  809.  
  810.         if( CIPFilter::AllowIP( StrtoIP(Host) ) &&
  811.             (CIPFilter::IsPrivateIP(StrtoIP(Host), StrtoIP(Host)) & (0x2|0x4)) == 0)        // if 0 or 1
  812.             return AddConnect(Host, Port); // DW
  813.     }
  814.   return FALSE; // DW
  815. }
  816.  
  817.  
  818. void CViewConnect::RemoveFirstNode()
  819. {
  820.     CString str_status = "Disconnecting";
  821.  
  822.     if(    m_lstConnected.GetItemText(0, m_lstConnected.GetColumnNumber( "Status" ) ).Find(str_status) == -1)
  823.         m_lstConnected.SetItemText(0, m_lstConnected.GetColumnNumber( "Status" ) , str_status);
  824. }
  825.  
  826.  
  827.  
  828. void CViewConnect::OnEditPaste() 
  829. {
  830.     // TODO: Add your command handler code here
  831.     
  832. }
  833.  
  834. void CViewConnect::OnChangeEditPort() 
  835. {
  836.     // TODO: If this is a RICHEDIT control, the control will not
  837.     // send this notification unless you override the CFormView::OnInitDialog()
  838.     // function and call CRichEditCtrl().SetEventMask()
  839.     // with the ENM_CHANGE flag ORed into the mask.
  840.     
  841.     // TODO: Add your control notification handler code here
  842.     
  843. }
  844.